using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class EndPVPMapSelect : MonoBehaviour
{
	[SerializeField]
	private int elementCount;

	[SerializeField]
	private float lowPercent = 0.3f;

	[SerializeField]
	private float highPercent = 0.7f;

	[SerializeField]
	private GameObject souceBtn;

	[SerializeField]
	private UIGrid gridScript;

	private List<EndPvpMapBtn> selectMapBtn = new List<EndPvpMapBtn>();

	public void Show()
	{
		List<ReputationMapInfo> list = new List<ReputationMapInfo>();
		list.Sort(delegate(ReputationMapInfo x, ReputationMapInfo y)
		{
			if (LowReputation(x) && LowReputation(y))
			{
				if (x.reputationPercent < y.reputationPercent)
				{
					return -1;
				}
				if (x.reputationPercent == y.reputationPercent)
				{
					return 0;
				}
				if (x.reputationPercent > y.reputationPercent)
				{
					return 1;
				}
			}
			if (LowReputation(x) && !LowReputation(y))
			{
				return -1;
			}
			if (!LowReputation(x) && LowReputation(y))
			{
				return 1;
			}
			if (HiReputation(x) && HiReputation(y))
			{
				if (x.reputationPercent < y.reputationPercent)
				{
					return 1;
				}
				if (x.reputationPercent == y.reputationPercent)
				{
					return 0;
				}
				if (x.reputationPercent > y.reputationPercent)
				{
					return -1;
				}
			}
			if (HiReputation(x) && !HiReputation(y))
			{
				return -1;
			}
			if (!HiReputation(x) && HiReputation(y))
			{
				return 1;
			}
			if (Captured(x) && Captured(y))
			{
				if (x.reputationPercent < y.reputationPercent)
				{
					return -1;
				}
				if (x.reputationPercent == y.reputationPercent)
				{
					return 0;
				}
				if (x.reputationPercent > y.reputationPercent)
				{
					return 1;
				}
			}
			if (Captured(x) && !Captured(y))
			{
				return -1;
			}
			if (!Captured(x) && Captured(y))
			{
				return 1;
			}
			if (Unlocked(x) && Unlocked(y))
			{
				if (x.reputationPercent < y.reputationPercent)
				{
					return 1;
				}
				if (x.reputationPercent == y.reputationPercent)
				{
					return 0;
				}
				if (x.reputationPercent > y.reputationPercent)
				{
					return -1;
				}
			}
			if (Unlocked(x) && !Unlocked(y))
			{
				return -1;
			}
			if (!Unlocked(x) && Unlocked(y))
			{
				return 1;
			}
			if (x.reputationPercent < y.reputationPercent)
			{
				return 1;
			}
			return (x.reputationPercent > y.reputationPercent) ? (-1) : 0;
		});
		list.RemoveAll((ReputationMapInfo x) => x.state == DistrictState.Locked);
		if (MapDataDownloader.instance != null)
		{
			list.RemoveAll((ReputationMapInfo x) => MapDataDownloader.instance.mapData.ContainsKey(x.sceneName) && !MapDataDownloader.instance.mapData[x.sceneName]);
		}
		list.RemoveAll((ReputationMapInfo x) => x.sceneName == SceneManager.GetActiveScene().name);
		List<ReputationMapInfo> list2 = list.FindAll((ReputationMapInfo x) => x.wasUnlocked);
		if (elementCount < list.Count)
		{
			list.RemoveRange(elementCount, list.Count - elementCount);
		}
		list2.Sort(delegate(ReputationMapInfo x, ReputationMapInfo y)
		{
			if (LowReputation(x) && LowReputation(y))
			{
				if (x.elite)
				{
					return -1;
				}
				if (x.elite && y.elite)
				{
					return 0;
				}
			}
			return 1;
		});
		if (list2.Count > 0)
		{
			for (int i = 0; i < list2.Count; i++)
			{
				if (!list.Contains(list2[i]))
				{
					if (list.Count == elementCount)
					{
						list.RemoveAt(list.Count - 1);
					}
					list.Add(list2[i]);
					list2.RemoveAt(i);
					break;
				}
			}
			for (int j = 0; j < list2.Count; j++)
			{
				if (list.Count >= elementCount)
				{
					break;
				}
				if (!list.Contains(list2[j]))
				{
					list.Add(list2[j]);
				}
			}
		}
		ShowBtn(list);
	}

	private void ShowBtn(List<ReputationMapInfo> districts)
	{
		SetButtonCount(districts.Count);
		for (int i = 0; i < districts.Count; i++)
		{
			selectMapBtn[i].SetMap(ManagerPVPLevels.instance.GetLevelInfo(districts[i].sceneName), districts[i], lowPercent, highPercent);
		}
	}

	private void SetButtonCount(int needCount)
	{
		if (selectMapBtn.Count < needCount)
		{
			for (int i = selectMapBtn.Count; i < needCount; i++)
			{
				selectMapBtn.Add(CreateButton());
			}
		}
		else if (selectMapBtn.Count > needCount)
		{
			int num = selectMapBtn.Count - needCount;
			for (int j = 0; j < num; j++)
			{
				GameObject obj = selectMapBtn[selectMapBtn.Count - 1].gameObject;
				selectMapBtn[selectMapBtn.Count - 1] = null;
				selectMapBtn.RemoveAt(selectMapBtn.Count - 1);
				Object.Destroy(obj);
			}
		}
		Sort();
	}

	private EndPvpMapBtn CreateButton()
	{
		GameObject gameObject = Object.Instantiate(souceBtn, Vector3.zero, Quaternion.identity) as GameObject;
		EndPvpMapBtn component = gameObject.GetComponent<EndPvpMapBtn>();
		gameObject.transform.parent = gridScript.transform;
		gameObject.SetActive(true);
		gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
		return component;
	}

	private void Sort()
	{
		if (gridScript != null)
		{
			gridScript.repositionNow = true;
		}
	}

	private bool Unlocked(ReputationMapInfo district)
	{
		return district.state == DistrictState.Unlocked;
	}

	private bool Captured(ReputationMapInfo district)
	{
		return district.state == DistrictState.Captured;
	}

	private bool LowReputation(ReputationMapInfo district)
	{
		return district.reputationPercent < lowPercent && Captured(district);
	}

	private bool HiReputation(ReputationMapInfo district)
	{
		return district.reputationPercent > highPercent && Unlocked(district);
	}
}
